<?php

/**
 * Description of Registry
 *
 * @author Ian Clarence <ian.clarence@gmail.com>
 */
class Registry extends ActiveRecord {

    /**
     * The table name.
     */
    const TABLE = "registry";

    /**
     * The constructor.
     *
     * @param array $fields
     */
    public function __construct(array $fields = array()) {
        parent::__construct($fields);
    }

    /**
     * Getting the value of $name from the registry, if present, otherwise return NULL.
     *
     * @param string $key
     * @return string
     */
    public static function getVal($key) {
        $registry = self::getReg($key);
        if ($registry != NULL) {
            return $registry->getValue();
        }
        return NULL;
    }

    /**
     * Getting the default value of $name from the registry, if present, otherwise return NULL.
     *
     * @param string $key
     * @return string
     */
    public static function getDef($key) {
        $registry = self::getReg($key);
        if ($registry != NULL) {
            return $registry->getDefVal();
        }
        return NULL;
    }

    /**
     * Setting the registry value of $name.
     * If there is no such registry value, then make one.
     *
     * @param string $key
     * @param string $value
     * @param string $defVal
     * @param string $path
     * @param int $priority
     * @return int
     */
    public static function setVal($key, $value, $defVal = NULL, $path = NULL, $priority = NULL, $name = NULL) {
        $registry = self::getReg($key);
        if ($registry != NULL) {
            if ($registry->getValue() == $value) {
                return $registry->getId();
            }
            // echo "Old value: " . $registry->getvalue() . " New value: " . $value . "\n";
            $registry->setValue($value);
            return $registry->save();
        }
        $registry = new Registry(
            array(
                "name" => ($name == NULL ? ucwords(strtolower(str_replace("_", " ", $key))) : $name),
                "key" => $key,
                "path" => ($path == NULL ? strtolower(str_replace("_", "/", $key)) : $name),
                "def_val" => $defVal,
                "value" => $value,
                "priority" => ($priority == NULL ? 1 : $priority)
            )
        );
        return $registry->save();
    }

    /**
     * Returning all the registry parameters for $key.
     * 
     * @param string $key
     * @return array
     */
    public static function getFlds($key) {
        $registry = self::getReg($key);
        if ($registry != NULL) {
            return $registry->getFields();
        }
        return NULL;
    }

    /**
     * Deleting a registry entry.
     * 
     * @param string $key
     * @return bool
     */
    public static function del($key) {
        $registry = self::getReg($key);
        if ($registry != NULL) {
            return self::delete($registry->getId());
        }
        return FALSE;
    }

    /**
     * Returning the registry object for $key.
     * 
     * @param string $key
     * @return Registry
     */
    private static function getReg($key) {
        $registries = self::search($key, "key", FALSE);
        if (count($registries) == 0) {
            return NULL;
        }
        $registry = $registries[0];
        if ($registry instanceof Registry) {
            $registry->loadAll();
            return $registry;
        }
        return NULL;
    }

    /**
     * Enabling getReg to be used outside this class. Just for diagnostics.
     * 
     * @param string $key
     * @return Registry
     */
    public static function getReggie($key) {
        return self::getReg($key);
    }

}